package com.example.wendao.view.ui.fragment;

import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wendao.App;
import com.example.wendao.R;
import com.example.wendao.databinding.FragmentSignInBinding;
import com.example.wendao.model.entity.User;
import com.example.wendao.viewmodel.UserViewModel;

public class Sign_inFragment extends Fragment {
    private FragmentSignInBinding fragmentSignInBinding;
    //返回我的页面

    //五颗星星
    public TextView star1;
    public TextView star2;
    public TextView star3;
    public TextView star4;
    public TextView star5;
    //先后提示文本框
    public TextView sign_cue1;
    public TextView sign_cue2;
    public TextView sign_cue3;


    public UserViewModel userViewModel;
    public User user;

    public Sign_inFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        fragmentSignInBinding= DataBindingUtil.inflate(inflater, R.layout.fragment_sign_in, container, false);
        View view=fragmentSignInBinding.getRoot();
        userViewModel = new ViewModelProvider(App.getInstance()).get(UserViewModel.class);

        star1 = view.findViewById(R.id.star1);
        star2 = view.findViewById(R.id.star2);
        star3 = view.findViewById(R.id.star3);
        star4 = view.findViewById(R.id.star4);
        star5 = view.findViewById(R.id.star5);
        sign_cue1 = view.findViewById(R.id.sign_cue1);
        sign_cue2 = view.findViewById(R.id.sign_cue2);
        sign_cue3 = view.findViewById(R.id.sign_cue3);
        /*空指针*/
        /*sign= fragmentSignInBinding.sign;
        have_sign = fragmentSignInBinding.haveSign;
        */
        user = new User();
        fragmentSignInBinding.sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //已登录状态下
                if (userViewModel.getCurrentUser() != null) {
                    user = userViewModel.getCurrentUser();
                    //签到满五天清除
                    if (user.getSigndays() == 5) {
                        user.setSigndays(1);//回到一天
                        star1.setBackground(getResources().getDrawable(R.drawable.sign_bright));
                        star2.setBackground(getResources().getDrawable(R.drawable.sign_dark));
                        star3.setBackground(getResources().getDrawable(R.drawable.sign_dark));
                        star4.setBackground(getResources().getDrawable(R.drawable.sign_dark));
                        star5.setBackground(getResources().getDrawable(R.drawable.sign_dark));

                        sign_cue1.setVisibility(View.INVISIBLE);
                        sign_cue2.setVisibility(View.VISIBLE);
                        sign_cue3.setVisibility(View.INVISIBLE);

                        fragmentSignInBinding.sign.setVisibility(View.INVISIBLE);
                        fragmentSignInBinding.haveSign.setVisibility(View.VISIBLE);
                    } else //不满五天继续积累
                    {
                        if (user.getSigndays() == 0)//本次第一次签到
                        {
                            star1.setBackground(getResources().getDrawable(R.drawable.sign_bright));
                            star2.setBackground(getResources().getDrawable(R.drawable.sign_dark));
                            star3.setBackground(getResources().getDrawable(R.drawable.sign_dark));
                            star4.setBackground(getResources().getDrawable(R.drawable.sign_dark));
                            star5.setBackground(getResources().getDrawable(R.drawable.sign_dark));

                            sign_cue1.setVisibility(View.INVISIBLE);
                            sign_cue2.setVisibility(View.VISIBLE);//签到成功
                            sign_cue3.setVisibility(View.INVISIBLE);

                            fragmentSignInBinding.sign.setVisibility(View.INVISIBLE);
                            fragmentSignInBinding.haveSign.setVisibility(View.VISIBLE);
                            user.setCoins(user.getCoins() + 1);//金币加一
                        } else if (user.getSigndays() == 1) //本次第二次签到
                        {
                            star1.setBackground(getResources().getDrawable(R.drawable.sign_bright));
                            star2.setBackground(getResources().getDrawable(R.drawable.sign_bright));
                            star3.setBackground(getResources().getDrawable(R.drawable.sign_dark));
                            star4.setBackground(getResources().getDrawable(R.drawable.sign_dark));
                            star5.setBackground(getResources().getDrawable(R.drawable.sign_dark));

                            sign_cue1.setVisibility(View.INVISIBLE);
                            sign_cue2.setVisibility(View.VISIBLE);
                            sign_cue3.setVisibility(View.INVISIBLE);

                            fragmentSignInBinding.sign.setVisibility(View.INVISIBLE);
                            fragmentSignInBinding.haveSign.setVisibility(View.VISIBLE);
                            user.setCoins(user.getCoins() + 1);
                        } else if (user.getSigndays() == 2) //本次第三次签到
                        {
                            star1.setBackground(getResources().getDrawable(R.drawable.sign_bright));
                            star2.setBackground(getResources().getDrawable(R.drawable.sign_bright));
                            star3.setBackground(getResources().getDrawable(R.drawable.sign_bright));
                            star4.setBackground(getResources().getDrawable(R.drawable.sign_dark));
                            star5.setBackground(getResources().getDrawable(R.drawable.sign_dark));

                            sign_cue1.setVisibility(View.INVISIBLE);
                            sign_cue2.setVisibility(View.VISIBLE);
                            sign_cue3.setVisibility(View.INVISIBLE);

                            fragmentSignInBinding.sign.setVisibility(View.INVISIBLE);
                            fragmentSignInBinding.haveSign.setVisibility(View.VISIBLE);
                            user.setCoins(user.getCoins() + 1);
                        } else if (user.getSigndays() == 3) //本次第四次签到
                        {
                            star1.setBackground(getResources().getDrawable(R.drawable.sign_bright));
                            star2.setBackground(getResources().getDrawable(R.drawable.sign_bright));
                            star3.setBackground(getResources().getDrawable(R.drawable.sign_bright));
                            star4.setBackground(getResources().getDrawable(R.drawable.sign_bright));
                            star5.setBackground(getResources().getDrawable(R.drawable.sign_dark));

                            sign_cue1.setVisibility(View.INVISIBLE);
                            sign_cue2.setVisibility(View.VISIBLE);
                            sign_cue3.setVisibility(View.INVISIBLE);

                            fragmentSignInBinding.sign.setVisibility(View.INVISIBLE);
                            fragmentSignInBinding.haveSign.setVisibility(View.VISIBLE);
                            user.setCoins(user.getCoins() + 1);
                        } else //本次第五次签到
                        {
                            star1.setBackground(getResources().getDrawable(R.drawable.sign_bright));
                            star2.setBackground(getResources().getDrawable(R.drawable.sign_bright));
                            star3.setBackground(getResources().getDrawable(R.drawable.sign_bright));
                            star4.setBackground(getResources().getDrawable(R.drawable.sign_bright));
                            star5.setBackground(getResources().getDrawable(R.drawable.sign_bright));

                            sign_cue1.setVisibility(View.INVISIBLE);
                            sign_cue2.setVisibility(View.INVISIBLE);
                            sign_cue3.setVisibility(View.VISIBLE);//五次获得优惠券

                            fragmentSignInBinding.sign.setVisibility(View.INVISIBLE);
                            fragmentSignInBinding.haveSign.setVisibility(View.VISIBLE);
                            user.setCoins(user.getCoins() + 1);
                            user.setCoupons(user.getCoupons() + 1);//优惠券添加
                        }
                        user.setSigndays(user.getSigndays() + 1);//天数加1

                    }
                    userViewModel.setCurrentUser(user);//返回当前用户的数据
                    userViewModel.updateUser(userViewModel.getCurrentUser());//把用户数据写进后台
                }
                //未登录状态
                else {
                    Toast.makeText(getActivity(), "登录之后才可以签到哦~", Toast.LENGTH_SHORT).show();
                }

            }
        });
        fragmentSignInBinding.signBackToMy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到上一页
                Navigation.findNavController(getView()).navigate(R.id.action_sign_inFragment_to_fragment_my);
                Toast.makeText(getActivity(), "111~", Toast.LENGTH_SHORT).show();

            }
        });

        return view;
    }
}